


package com.starmark.gateway.project.provider.web;

import com.starmark.auth.common.util.UserUtil;
import com.starmark.core.framework.base.AbstractController;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.gateway.project.provider.service.IGatewayAppService;
import com.starmark.gateway.project.provider.vo.GatewayAppVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 网关应用 前端控制器
 * </p>
 *
 * @author starmark
 * @since 2020-01-24
 */
@RestController
@Api(value = "网关应用管理", description = "网关应用管理")
@RequestMapping("gateway/app")
public class GatewayAppController extends AbstractController {

    @Autowired
    IGatewayAppService gatewayAppService;

    @ApiOperation(value = "查询网关应用列表")
    @PostMapping(value = "/query")
    public Object query(@RequestBody Map<String, Object> param) {
        param.put("projectCode", UserUtil.getCurrentUser().getProjectCode());
        return BeanMapper.mapPage(gatewayAppService.query(param), GatewayAppVo.class);
    }


    @ApiOperation(value = "新增网关应用")
    @PostMapping
    public GatewayAppVo add(@RequestBody GatewayAppVo gatewayAppVo) {

        gatewayAppVo.setProjectCode(UserUtil.getCurrentUser().getProjectCode());
        gatewayAppVo.setId(gatewayAppService.addApp(gatewayAppVo));
        return gatewayAppVo;
    }

    @ApiOperation(value = "网关应用详情")
    @GetMapping(value = "/{id}")
    public GatewayAppVo get(@PathVariable("id") String id) {

        return gatewayAppService.getApp(id);
    }

    @PutMapping
    @ApiOperation(value = "修改网关应用")
    public GatewayAppVo update(@RequestBody GatewayAppVo gatewayAppVo) {
        gatewayAppVo.setProjectCode(UserUtil.getCurrentUser().getProjectCode());
        gatewayAppService.updateApp(gatewayAppVo);
        return gatewayAppVo;
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除网关应用")
    public void delete(@PathVariable("id") String id) {

        gatewayAppService.delete(id);
    }


}
